package adapters;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.sxpjyy.monkey.mobilenurse.R;

import java.util.List;

import objects.CostItem;

/**
 * Created by Monkey on 2017/3/9.
 * 消费项目适配器
 */

class CostItemAdapter extends BaseAdapter {
    private List<CostItem> list;
    private LayoutInflater inflater;
    private Context mContext;

    CostItemAdapter(Context context) {
        mContext = context;
        this.inflater = LayoutInflater.from(mContext);
    }

    public void setList(List<CostItem> list) {
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list != null && list.size() > 0) {
            return list.size();
        }
        return 0;
    }

    @Override
    public Object getItem(int i) {
        if (list != null && list.get(i) != null) {
            return list.get(i);
        }
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater.inflate(R.layout.item_cost_1, parent, false);
            holder.cost_name = (TextView) convertView.findViewById(R.id.cost_name);
            holder.cost_quantity = (TextView) convertView.findViewById(R.id.cost_quantity);
            holder.cost_byself = (TextView) convertView.findViewById(R.id.cost_byself);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        if (list != null && list.size() > 0) {
            if (list.get(position) != null) {
                CostItem costItem = list.get(position);
                if (costItem.getCost_name() != null) {
                    holder.cost_name.setText(costItem.getCost_name());
                }
                if (costItem.getCost_quantity() != null) {
                    holder.cost_quantity.setText(costItem.getCost_quantity());
                }
                if (costItem.getCost_byself() != null) {
                    holder.cost_byself.setText(costItem.getCost_byself());
                }
            }
        }
        return convertView;
    }

    private class ViewHolder {
        private TextView cost_name;
        private TextView cost_quantity;
        private TextView cost_byself;
    }
}
